const signUpButton = document.getElementById('signUp')
const signInButton = document.getElementById('signIn')
const container = document.getElementById('container')
const signbtn=$("#signbtn");
const stel=$("#stel");
const spwd=$("#spwd");
const sspwd=$("#sspwd");
const SverifyCode=$("#SverifyCode");
const SUError=$("#SUError");

signUpButton.addEventListener('click', () => {
    container.classList.add("right-panel-active")
})

signInButton.addEventListener('click', () => {
    container.classList.remove("right-panel-active")
})


signbtn.click(function () {
    if (stel.val() === "") {
        stel.addClass("input change");
        stel.attr("placeholder", "手机号不能为空!");
        return;
    }
    if (spwd.val() === "") {
        spwd.addClass("input change");
        spwd.attr("placeholder", "密码不能为空!");
        return;
    }
    if (sspwd.val() === "") {
        sspwd.addClass("input change");
        sspwd.attr("placeholder", "确认密码不能为空!");
        return;
    }
    if (SverifyCode.val() === "") {
        SverifyCode.addClass("input change");
        SverifyCode.attr("placeholder", "验证码不能为空!");
        return;
    }

    // 校验手机号格式
    const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if (!reg.test(stel.val())) {
        stel.val(null);
        stel.addClass("input change");
        stel.attr("placeholder", "手机号格式错误!");
        return;
    }

    const reg1 = /^[a-zA-Z0-9]{6,18}$/
    if (!reg1.test(spwd.val())) {
        spwd.val(null);
        spwd.addClass("input change");
        spwd.attr("placeholder", "格式错误,格式为6-18位字母和数字的组合!");
        return;
    }

    if (sspwd.val() !== sspwd.val()) {
        sspwd.val(null);
        sspwd.addClass("input change");
        sspwd.attr("placeholder", "两次密码不一致!");
        return;
    }

    // 发送Ajax请求
    $.ajax({
        url: "/users/sign",
        type: "post",
        data: {
            tel: stel.val(),
            pwd: spwd.val(),
            uType: $("#enr").val(),
            verifyCode: SverifyCode.val()
        },
        success: function (Result) {
            SUError.css("visibility", "visible");
            SUError.text(Result.msg);
            const time = new Date().getTime(); // 获取当前时间戳
            const SverifyCodeImg = document.querySelector('#SverifyCodeImg');
            SverifyCodeImg.src = '/verifyCode?t=' + time; // 设置验证码图片地址
        },
        error: function () {
            alert("注册失败,系统忙,请稍后重试!");
        }
    });

});

const loginbtn=$("#loginbtn");
const lname=$("#lname");
const lpwd=$("#lpwd");
const verifyCode=$("#verifyCode");
const LUError=$("#LUError");

loginbtn.click(function () {
    if (lname.val() === "") {
        lname.addClass("input change");
        lname.attr("placeholder", "手机号不能为空!");
        return;
    }
    if (lpwd.val() === "") {
        lpwd.addClass("input change");
        lpwd.attr("placeholder", "密码不能为空");
        return;
    }
    if (verifyCode.val() === "") {
        verifyCode.addClass("input change");
        verifyCode.attr("placeholder", "验证码不能为空");
        return;
    }

    // 校验手机号格式
    const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if (!reg.test(lname.val())) {
        lname.val(null);
        lname.addClass("input change");
        lname.attr("placeholder", "手机号格式错误!");
        return;
    }
    // 获取“记住我”复选框的状态
    const rememberMeCheckbox = document.querySelector('input[name="rememberMe"]');
    const rememberMe = rememberMeCheckbox.checked ? 1 : 0;
    // 发送Ajax请求
    $.ajax({
        url: "/users/login",
        type: "post",
        data: {
            tel: lname.val(),
            pwd: lpwd.val(),
            rememberMe:rememberMe,
            verifyCode: verifyCode.val()
        },
        success: function (Result) {
          verifyCode.val(null);
            if(Result.code !== 30011){
                LUError.css("visibility", "visible");
                LUError.text(Result.msg);
            }
            const time = new Date().getTime(); // 获取当前时间戳
            const verifyCodeImg = document.querySelector('#verifyCodeImg');
            verifyCodeImg.src = '/verifyCode?t=' + time; // 设置验证码图片地址
            if (Result.code === 30011) {
                $("#app").css("display","block");
                setTimeout(function() {
                    $("#app").css("display","none");
                  window.location.href="/users/main";
                }, 1000);
            }
        },
        error: function () {
            alert("请求失败");
        }
    });
});

function refreshVerifyCode() {
    const time = new Date().getTime(); //获取当前时间戳
    const verifyCodeImg = document.querySelector('#verifyCodeImg');
    verifyCodeImg.src = '/verifyCode?t=' + time; //设置验证码图片地址
}

function refreshSVerifyCode() {
    const time = new Date().getTime(); //获取当前时间戳
    const SverifyCodeImg = document.querySelector('#SverifyCodeImg');
    SverifyCodeImg.src = '/verifyCode?t=' + time; //设置验证码图片地址
}

window.onload = function () {
    const time = new Date().getTime(); // 获取当前时间戳
    const verifyCodeImg = document.querySelector('#verifyCodeImg');
    const SverifyCodeImg = document.querySelector('#SverifyCodeImg');
    verifyCodeImg.src = '/verifyCode?t=' + time; // 设置验证码图片地址
    SverifyCodeImg.src = '/verifyCode?t=' + time;
    if($("#msg").val()!==""){
        alert($("#msg").val());
        $("#msg").val("");
    }
}

// 获取用户信息
// function getUser() {
//     let user = null;
//     const cookies = document.cookie.split(";");
//
//     // 遍历cookie，查找'user'字段
//     for (let i = 0; i < cookies.length; i++) {
//         const cookie = cookies[i].trim();
//         if (cookie.indexOf("user=") == 0) { // 找到'user'字段
//             user = JSON.parse(cookie.substring("user=".length, cookie.length));
//             break;
//         }
//     }
// }